package org.ace.biz.user.impl;

import io.netty.channel.Channel;

import java.util.List;

import javax.annotation.Resource;

import org.ace.basicdata.IBasicDataCentral;
import org.ace.basicdata.model.PlayerBasicData;
import org.ace.biz.user.IPlayerBiz;
import org.ace.cache.user.IPlayerCache;
import org.ace.constans.GameConstans;
import org.ace.dao.model.Player;
import org.ace.model.DressEquipment;
import org.ace.serializer.MySqlSerializer;
import org.ace.tool.impl.PropertyUtil;
import org.springframework.stereotype.Service;

@Service("playerBiz")
public class PlayerBiz implements IPlayerBiz {

	private PropertyUtil propertyUtil;
	
	@Resource(name="playerCache")
	private IPlayerCache playerCache;
	
	@Resource(name="playerBasicDataCenteral")
	private IBasicDataCentral<PlayerBasicData> playerBasicDataCenteral;
	
	public List<Player> getList(Integer accId) {
		return playerCache.getList(accId);
				
	}
    
	//具体需求可以在该，d
	public boolean create(int job, String name, Integer accId) {
		//职业是否合法
		if(!playerBasicDataCenteral.has(job))
		 return false;
		//是否重命名
		if(playerCache.hasName(name))
			return false;
		//当前账号下的角色数量是否已达上限
		if(getList(accId).size()>=GameConstans.PLAREY_LIST_MAX)
			return false;
		//配置表获取玩家基本信息
		PlayerBasicData data=playerBasicDataCenteral.get(job);
		 //设置一个绝色基本信息
		Player player=new Player(data);
		player.setId(accId);
		player.setUsername(name);
		//区别于值类型 引用类型是要赋值的 
		DressEquipment equi=new DressEquipment();
		
		byte[] byteArray = MySqlSerializer.serialize(equi);
		player.setEquipment(byteArray);
		
		//创建角色并存入缓存和数据库
		playerCache.save(player);
		//获取角色id 现在是当账号单角色
		//int playerId= playerCache.getList(accId).get(0).getUserid();
		return true;
	}
   
	@Override
	public boolean select(int playerId, Channel channel, Integer accId) {
		//当前账号下是否有此角色, 必要的检测
		if(playerCache.isOnLine(accId)) return false;
		if(playerCache.hasPlayer(accId, playerId))
		{//已存在的化,将玩家角色id和channel绑定
			playerCache.onLine(channel, playerId);
			return true;
		}
		return false;
	}

	@Override
	public Integer getPlayerId(Channel channel) {
		return playerCache.getPlayerId(channel);
	}

	@Override
	public Channel getChannel(Integer playerId) {
		return playerCache.getChannel(playerId);
	}

	@Override
	public Player get(Integer playerId) {
		return playerCache.get(playerId);
	}

	@Override
	public void offLine(Channel channel) {
		playerCache.offLine(channel);

	}

	@Override
	public void changeMap(Integer playerId, int map, float x, float y, float z) {
		Player player=get(playerId);
		player.setMap(map);
		player.setX(x);
		player.setY(y);
		player.setZ(z);
		playerCache.update(player);
		
	}

	@Override
	public boolean addExp(Integer playerId, Integer addValue) {
		Player player = get(playerId);
		//经验最高值是有算法随等级提升而提升的，可以通过封装
		int expMax = propertyUtil.getExpMax(player);
		int nowExp = player.getExp();
		
		boolean levelUp = false;
		PlayerBasicData data= playerBasicDataCenteral.get(player.getUserjob());
	    //升级更新数据
		while(nowExp+ addValue>= expMax)
		{
			levelUp=true;
			player.setAtk(propertyUtil.getAtk(player, data));
			player.setDef(propertyUtil.GetDef(player, data));
			player.setHpmax(propertyUtil.getHp(player, data));
			player.setHp(player.getHpmax());
			player.setExp(0);
			player.setLv(player.getLv()+1);
			addValue-=expMax-nowExp;
			expMax=propertyUtil.getExpMax(player);
			nowExp=0;	
		}
		playerCache.update(player);
		return levelUp;//升级了返回true 没有 false
	}

	@Override
	public void hpChange(Integer playerId, Integer changeValue) {
		Player player = get(playerId);
		if(player.getHp() + changeValue <0)
		{
			player.setHp(0);//死亡
		}else if(player.getHp()+ changeValue>player.getHpmax())
		{
			player.setHpmax(player.getHpmax());
		}
		else
		{
			player.setHp(player.getHp()+changeValue);
		}

	}

	@Override
	public boolean spendMoney(Integer playerId, Integer value) {
		Player player =get(playerId);
		if(player.getMoney()>=value)
		{
			player.setMoney(player.getMoney()-value);
			return true;
		}
		return false;
	}

	@Override
	public Integer getUserId(Channel channel) {
		return playerCache.getPlayerId(channel);
	}

	

}
